package com.top.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.top.entities.BladeDatabaseAudit;
import com.top.entities.DatabaseAudit;
import com.top.entities.Query;
import com.top.entities.R;
import com.top.service.IBladeDatabaseAuditService;
import com.top.vo.DatabaseAuditVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangjiawen
 * @since 2020-08-09
 */

@RestController
@AllArgsConstructor
@RequestMapping("/dataAudit")
@CrossOrigin
@Slf4j
public class BladeDatabaseAuditController {
    private IBladeDatabaseAuditService databaseAuditService;

    /**
     * 查询多条(分页)
     */
    @GetMapping("/list")
    public R<IPage<DatabaseAuditVo>> list(@RequestParam Map<String, Object> databaseAudit, Query query) {
        databaseAudit.remove("current");
        databaseAudit.remove("size");
        databaseAudit.remove("time");
        Object time1 = databaseAudit.get("time1");
        Object time2 = databaseAudit.get("time2");
        //log.info(Convert.toDate(time1)+"    "+Convert.toDate(time2));

        Page page = new Page((long) Convert.toInt(query.getCurrent(), 1), (long)Convert.toInt(query.getSize(), 10));
        QueryWrapper queryWrapper = new QueryWrapper(databaseAudit);
    if(time1!=null && time2!=null){
        queryWrapper.between("TIME",time1,time2);
    }
        queryWrapper.orderByDesc("TIME");

        IPage<BladeDatabaseAudit> pages = databaseAuditService.page(page, queryWrapper);
        List<DatabaseAuditVo> records = pages.getRecords().stream().map(databaseAuditApi -> {
            DatabaseAuditVo vo = new DatabaseAuditVo();
            BeanUtil.copyProperties(databaseAuditApi,vo);
            vo.setStrId(Convert.toStr(databaseAuditApi.getId()));
            return vo;
        }).collect(Collectors.toList());
        IPage<DatabaseAuditVo> pageVo = new Page<>(pages.getCurrent(), pages.getSize(), pages.getTotal());
        pageVo.setRecords(records);
        return R.data(pageVo);
    }

}

